/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TTTSettings.java
 *
 * Created on 28.08.2009, 10:31:01
 */

package com.jtalker.client.plugins.TicTacToe;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.DefaultComboBoxModel;
import javax.swing.GroupLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.LayoutStyle;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;

/**
 * 
 * @author christoph
 */
public class TTTSettings extends JFrame {
	private boolean cancelled = false;
	private int gameType = 0;
	private String selectedUser = "";
	private int selectedAI = 0;
	private TicTacToe myPlugin;

	/** Creates new form TTTSettings */
	public TTTSettings(TicTacToe myPlugin) {
		this.myPlugin = myPlugin;
		initComponents();
	}

	/**
	 * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The
	 * content of this method is always regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
	private void initComponents() {

		jLabelTTT = new JLabel();
		jTabbedPaneWithUser = new JTabbedPane();
		jPanelWithUser = new JPanel();
		jLabelUserList = new JLabel();
		jComboBoxUsers = new JComboBox();
		jScrollInfoPane = new JScrollPane();
		jTextInfoArea = new JTextArea();
		jLabelEnemyState = new JLabel();
		jPanelWithAI = new JPanel();
		jLabelAIQuality = new JLabel();
		jComboBoxAILevel = new JComboBox();
		jButtonOK = new JButton();
		jButtonCancel = new JButton();

		setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);

		jLabelTTT.setFont(new Font("Dialog", 1, 18));
		jLabelTTT.setHorizontalAlignment(SwingConstants.CENTER);
		jLabelTTT.setIcon(new ImageIcon(getClass().getResource("ttticon.png"))); // NOI18N
		jLabelTTT.setText("Tic-Tac-Toe");

		jLabelUserList.setText("Select user:");
		jComboBoxUsers.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				changeUser(evt);
			}
		});

		jTextInfoArea.setColumns(20);
		jTextInfoArea.setEditable(false);
		jTextInfoArea.setRows(5);
		jTextInfoArea
				.setText("Note: you can only play against users which: \n- are online \n- have the TicTacToe plugin installed");
		jTextInfoArea.setBorder(null);
		jTextInfoArea.setFocusable(false);
		jTextInfoArea.setOpaque(false);
		jTextInfoArea.setRequestFocusEnabled(false);
		jScrollInfoPane.setViewportView(jTextInfoArea);

		GroupLayout jPanelWithUserLayout = new GroupLayout(jPanelWithUser);
		jPanelWithUser.setLayout(jPanelWithUserLayout);
		jPanelWithUserLayout.setHorizontalGroup(jPanelWithUserLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(
				GroupLayout.Alignment.TRAILING,
				jPanelWithUserLayout
						.createSequentialGroup()
						.addContainerGap()
						.addGroup(
								jPanelWithUserLayout
										.createParallelGroup(GroupLayout.Alignment.TRAILING)
										.addComponent(jScrollInfoPane, GroupLayout.Alignment.LEADING,
												GroupLayout.DEFAULT_SIZE, 387, Short.MAX_VALUE)
										.addComponent(jLabelEnemyState, GroupLayout.Alignment.LEADING,
												GroupLayout.DEFAULT_SIZE, 387, Short.MAX_VALUE)
										.addGroup(
												jPanelWithUserLayout
														.createSequentialGroup()
														.addComponent(jLabelUserList)
														.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 48,
																Short.MAX_VALUE)
														.addComponent(jComboBoxUsers, GroupLayout.PREFERRED_SIZE, 269,
																GroupLayout.PREFERRED_SIZE))).addContainerGap()));
		jPanelWithUserLayout.setVerticalGroup(jPanelWithUserLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(
				jPanelWithUserLayout
						.createSequentialGroup()
						.addContainerGap()
						.addGroup(
								jPanelWithUserLayout
										.createParallelGroup(GroupLayout.Alignment.BASELINE)
										.addComponent(jComboBoxUsers, GroupLayout.PREFERRED_SIZE,
												GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
										.addComponent(jLabelUserList))
						.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
						.addComponent(jScrollInfoPane, GroupLayout.DEFAULT_SIZE, 83, Short.MAX_VALUE)
						.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addComponent(jLabelEnemyState)
						.addContainerGap()));

		jTabbedPaneWithUser.addTab("Game against real user", jPanelWithUser);

		jLabelAIQuality.setText("AI quality:");
		jComboBoxAILevel.setModel(new DefaultComboBoxModel(new String[] { "Hard", "Normal", "Stupid" }));
		GroupLayout jPanelWithAILayout = new GroupLayout(jPanelWithAI);
		jPanelWithAI.setLayout(jPanelWithAILayout);
		jPanelWithAILayout.setHorizontalGroup(jPanelWithAILayout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(
				jPanelWithAILayout.createSequentialGroup().addContainerGap().addComponent(jLabelAIQuality)
						.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 54, Short.MAX_VALUE)
						.addComponent(jComboBoxAILevel, GroupLayout.PREFERRED_SIZE, 270, GroupLayout.PREFERRED_SIZE)
						.addContainerGap()));
		jPanelWithAILayout.setVerticalGroup(jPanelWithAILayout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(
				jPanelWithAILayout
						.createSequentialGroup()
						.addContainerGap()
						.addGroup(
								jPanelWithAILayout
										.createParallelGroup(GroupLayout.Alignment.BASELINE)
										.addComponent(jLabelAIQuality)
										.addComponent(jComboBoxAILevel, GroupLayout.PREFERRED_SIZE,
												GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
						.addContainerGap(107, Short.MAX_VALUE)));

		jTabbedPaneWithUser.addTab("Game against computer (AI)", jPanelWithAI);

		jButtonOK.setText("OK - Start game");
		jButtonOK.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				startGame(evt);
			}
		});

		jButtonCancel.setText("Cancel");
		jButtonCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				cancelSetting(evt);
			}
		});

		GroupLayout layout = new GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(
				GroupLayout.Alignment.TRAILING,
				layout.createSequentialGroup()
						.addContainerGap()
						.addGroup(
								layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
										.addComponent(jTabbedPaneWithUser, GroupLayout.Alignment.LEADING,
												GroupLayout.DEFAULT_SIZE, 416, Short.MAX_VALUE)
										.addComponent(jLabelTTT, GroupLayout.Alignment.LEADING, GroupLayout.DEFAULT_SIZE,
												416, Short.MAX_VALUE)
										.addGroup(
												layout.createSequentialGroup().addComponent(jButtonOK)
														.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
														.addComponent(jButtonCancel))).addContainerGap()));
		layout.setVerticalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(
				layout.createSequentialGroup()
						.addContainerGap()
						.addComponent(jLabelTTT, GroupLayout.PREFERRED_SIZE, 26, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
						.addComponent(jTabbedPaneWithUser, GroupLayout.DEFAULT_SIZE, 170, Short.MAX_VALUE)
						.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
						.addGroup(
								layout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(jButtonCancel)
										.addComponent(jButtonOK)).addContainerGap()));

		pack();
	}// </editor-fold>//GEN-END:initComponents

	private void changeUser(ActionEvent evt) {// GEN-FIRST:event_jComboBoxUsersActionPerformed
		// TODO add your handling code here:
	}// GEN-LAST:event_jComboBoxUsersActionPerformed

	private void startGame(ActionEvent evt) {// GEN-FIRST:event_jButtonOKActionPerformed
		this.cancelled = false;
		this.gameType = this.jTabbedPaneWithUser.getSelectedIndex();
		this.selectedUser = (String) this.jComboBoxUsers.getSelectedItem();
		this.selectedAI = this.jComboBoxAILevel.getSelectedIndex();

		this.myPlugin.startNewGame(false);
		this.setVisible(false);
	}// GEN-LAST:event_jButtonOKActionPerformed

	private void cancelSetting(ActionEvent evt) {// GEN-FIRST:event_jButtonCancelActionPerformed
		this.cancelled = true;
		this.setVisible(false);
	}// GEN-LAST:event_jButtonCancelActionPerformed

	public void setUserList(ArrayList<String> list) {
		System.out.println("Setting List.: " + list.toString());

		try {
			// String[] list = _list.toArray();
			this.jComboBoxUsers.setModel(new DefaultComboBoxModel(list.toArray()));
		} catch (Exception e) {
			System.out.println("Exception: " + e.toString());
		}
	}

	public int getGameType() {
		return this.gameType;
	}

	public String getSelectedUser() {
		return this.selectedUser;
	}

	public int getSelectedAI() {
		return selectedAI;
	}

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private JButton jButtonCancel;
	private JButton jButtonOK;
	private JComboBox jComboBoxAILevel;
	private JComboBox jComboBoxUsers;
	private JLabel jLabelTTT;
	private JLabel jLabelUserList;
	private JLabel jLabelAIQuality;
	private JLabel jLabelEnemyState;
	private JPanel jPanelWithUser;
	private JPanel jPanelWithAI;
	private JScrollPane jScrollInfoPane;
	private JTabbedPane jTabbedPaneWithUser;
	private JTextArea jTextInfoArea;
	// End of variables declaration//GEN-END:variables

}
